package com.atguigu.gulimall.auth.web;


import com.atguigu.gulimall.auth.config.GiteeProperties;
import com.atguigu.gulimall.auth.util.GiteeProvider;
import com.atguigu.gulimall.auth.util.GiteeUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author ming
 * @create 2022/5/6
 * @description:
 */
@RequestMapping("/ucenter/gitee")
@Controller
@Slf4j
public class ApiGiteeController {
    @Autowired
    private GiteeProperties accessTokenDTO;
    @Autowired
    private GiteeProvider provider;
    @GetMapping("/login")
    public String giteeLogin() {
        return "redirect:https://gitee.com/oauth/authorize?client_id="
                + accessTokenDTO.getClientId() + "&redirect_uri=" + accessTokenDTO.getRedirectUri() + "&response_type=code&state=1";
    }

    @GetMapping("/callback")
    public String callback(@RequestParam(name = "code") String code,
                           @RequestParam(name = "state") String state) {
        accessTokenDTO.setCode(code);
        accessTokenDTO.setState(state);
        String token = provider.getAccessToken(accessTokenDTO);
        GiteeUser giteeUser = provider.getGiteeUser(token);
        System.out.println("id  :" + giteeUser.getId());
        System.out.println("name  :" + giteeUser.getName());
        System.out.println("bio  :" + giteeUser.getBio());
        return "redirect:http://gulimall.com";
    }
}

    
